<?php	
require_once(APPLICATION_DIR. "/library/external/PHPMailer/class.phpmailer.php");

if (!defined(NO_MAIL_HOST_NAME))
{
	define("NO_MAIL_HOST_NAME", "You must provide a mail server.");
}
if (!defined(NO_MAIL_USER_NAME))
{
	define("NO_MAIL_USER_NAME", "You must provide a username on server.");
}
if (!defined(AT_LEAST_ONE_TOADDRESS))
{
	define("NO_MAIL_USER_NAME", "You must provide at least one recipient email address");
}
if (!defined(ALTER_HTML_BODY)) {
    define("ALTER_HTML_BODY", "To view the message, please use an HTML compatible email viewer!");
}
if (!defined(CANT_ATTACH_FILE)) {
	define("CANT_ATTACH_FILE","One or more of your file cannot been attached. 
	                           Please make sure the file exist and could be accessed.");
}
if (!defined(PROPER_MAIL_ARGUMENT)) 
{
	define("PROPER_MAIL_ARGUMENT", "OK");
}
class IO_Mail extends PHPMailer
{
    /*
    * This class was created to simplify the send mail action,
    * :TODO:
    * - Create an object.
    * - Call SendEmail method and sass arguments to it.
    * - The result can be retrieve by reading "SendResult" properties.
    * 
    * Arguments description:  array of arguments $args
      The following arguments are required:
    *     - string  args['host']          : mail server address.     
    *     - string  args['username']      : username on server.          
    *     - string  args['to']			  : recipient address(es). (require at least one)
    *     - string  args['body']          : message body.
    *
    * The following arguments are often provided:
    *     - string  args['password']      : username's password.
    *     - int     args['port']          : port number. Default is 25 (SMTP).
    *
    * Below arguments are optional:
    *     - string  args['method']        : method to send mail, default is SMTP.
    *     - string  args['smtpSecure']    : security method.
    *     - bool    args['smtpAuth']      : use or not use Authentication.
    *     - bool    args['isHtml']        : Specify content type is HTML or not, default is HTML.
    *     - string  args['attachments']   : path to an attachments.
    *
    * Those format of args['to'], args['cc'], args['bcc'], args['reply'] are acceptable:
    *     - an array of email address.
    *		Example:
    *		[0] => "person0@domain.com", [1] => "person1@domain.com" ...
    *
    *     - an array of email address and name.
    *       Example:
    *       ['address'][0] => "person0@domain.com", ['address'][1] => "person1@domain.com" ...
    *       ['name']   [0] => "person0"           , ['name']   [1] => "person1" ...
    *
    *     - a string of email adddress.
    *
    * To attach file(s), use args['attachments] as one of three following formats :
    *     - An array of path, name, encoding, type. For example:
    *       ['path']    [0]=> "C:\path0.xxx"      , ['path']    [1]=> "C:\path1.xxx"
    *       ['name']    [0]=> "XXX file"          , ['name']    [1]=> "XXX file 2"
    *       ['encoding'][0]=> "base64"            , ['encoding'][1]=> "8bit"
    *       ['type']    [0]=> "application/octet-stream"
    *       ['type']    [1]=> "application/octet-stream"
    *
    *     - An array of path: [0] => "C:\path0.xxx", [1]=> "C:\path1.xxx"
    *	  - A string (path to attach file).
    */
	public $SendResult = '';
    
    function SendEmail($args) 
    {    	
    	$this->SetServerInfo($args);    	  	
    	$this->SetSenderInfo($args);
    	$this->SetMailRecipients($args);
    	$this->SetMailContent($args);
    	$this->SetAttachments($args);    	
    	
    	if(!$this->Send()) 
        {        	
        	$this->SendResult = $this->ErrorInfo;
		    return false;
		}
		else
		{         
			$this->SendResult = "Message sent!";
            return true;
        }
    }
    
    function SetServerInfo($args)
    {
        extract($args);
		
        if (isset($method))
        {            
            $this->Mailer = $method;
        }
        else 
        {
        	$this->IsSMTP();
        }
        
        if (isset($host))
        {
            $this->Host = $host;
        }
        else
        {
        	$this->SendResult = NO_MAIL_HOST_NAME;
        	return false;
        }

        if (isset($port))
        {
            $this->Port = $port;
        }

        if (isset($smtpAuth))
        {
            $this->SMTPAuth = $smtpAuth;
        }

        if (isset($smtpSecure))
        {
            $this->SMTPSecure = $smtpSecure;
        }
        
        if (isset($smtpKeepAlive))
        {
        	$this->SMTPKeepAlive = $smtpKeepAlive;
        }
        
        $this->SendResult = PROPER_MAIL_ARGUMENT;
        return true;
    }

    function SetSenderInfo($args)
    {
        extract($args);
                
        if (isset($username))
        {
            $this->Username = $username;
        }
        else
        {
            $this->SendResult = NO_MAIL_USER_NAME;
            return false;
        }

        if (isset($password))
        {
            $this->Password   = $password;
        }

        if (isset($from))
        {
            $this->From = $from;
        }
        else
        {
            $this->From = $username;
        }

        if (isset($senderName))
        {
            $this->FromName = $senderName;
        }
        else
        {
            $this->FromName = $username;
        }

        if (isset($senderEmail)) 
        {
            $this->Sender = $senderEmail;
        }
        else
        {
            $this->Sender = $username;
        }
        
        $this->SendResult = PROPER_MAIL_ARGUMENT;
        return true;
    }

    function SetMailRecipients($args)
    {
        extract($args);
        
        if (is_array($to['address']))
        {
            extract($to);            
        	foreach ($address as $key => $value)
            {
                if (!$name[$key])
                {
                    $name[$key] = $address[$key];
                }
                $this->AddAddress($address[$key], $name[$key]);
            }
        }
        else if (is_array($to))
        {
            foreach ($to as $key => $value)
            {
                $this->AddAddress($value,$value);
            }
        }
        else if (isset($to))
        {
        	$this->AddAddress($to,$to);
        }
        else 
        {
        	$this->SendResult = AT_LEAST_ONE_TOADDRESS;
        	return false;
        }

        
        if (is_array($cc['address']))
        {
            extract($cc);            
        	foreach ($address as $key => $value)
            {
                if (!$name[$key])
                {
                    $name[$key] = $address[$key];
                }
                $this->AddCC($address[$key], $name[$key]);
            }
        }
        else if (is_array($cc))
        {
            foreach ($cc as $key => $value)
            {
                $this->AddCC($value,$value);
            }
        }
        else if (isset($cc))
        {
        	$this->AddCC($cc,$cc);
        }
        
        
        if (is_array($bcc['address']))
        {
            extract($bcc);            
        	foreach ($address as $key => $value)
            {
                if (!$name[$key])
                {
                    $name[$key] = $address[$key];
                }
                $this->AddBCC($address[$key], $name[$key]);
            }
        }
        else if (is_array($bcc))
        {
            foreach ($bcc as $key => $value)
            {
                $this->AddBCC($value,$value);
            }
        }
        else if (isset($bcc))
        {
        	$this->AddBCC($bcc,$bcc);
        }
        
        if (is_array($reply['address']))
        {
            extract($reply);            
        	foreach ($address as $key => $value)
            {
                if (!$name[$key])
                {
                    $name[$key] = $address[$key];
                }
                $this->AddBCC($address[$key], $name[$key]);
            }
        }
        else if (is_array($reply))
        {
            foreach ($reply as $key => $value)
            {
                $this->AddBCC($value,$value);
            }
        }
        else if (isset($reply))
        {
        	$this->AddBCC($reply,$reply);
        }
               
        $this->SendResult = PROPER_MAIL_ARGUMENT;
        return true;
    }

    function SetMailContent($args)
    {
        extract($args);
        if (isset($subject))
        {
            $this->Subject    = $subject;
        }

        if (!isset($body)) 
        {
            $this->SendResult = MESSAGE_BODY_EMPTY;
            return false;
        }
        
        if (is_bool($isHtml) &&($isHtml == false))
        {
        	$this->IsHTML(false);
        	$this->Body = $body;
        }
        else        
        {
        	$this->IsHTML();
            $this->MsgHTML($body);
            if (isset($altbody))
            {
                $this->AltBody = $altbody;
            }
            else
            {
                $this->AltBody = ALTER_HTML_BODY;
            }
        }
        
        $this->SendResult = PROPER_MAIL_ARGUMENT;
        return true;
    }

    function SetAttachments($args)
    {
        extract($args);
        
        if (is_array($attachments['path'])) 
        {
        	extract($attachments);
        	foreach ($path as $key => $value) 
            {   
            	if (!isset($encoding[$key])) {
            		$encoding[$key]='base64';
            	}
            	if (!isset($type[$key])) {
            		$type[$key] = 'application/octet-stream';
            	}
                $this->AddAttachment($value,$name[$key],$encoding[$key],$type[$key]);                
            }
        }
        else if (is_array($attachments))
        {
        	foreach ($attachments as $key => $value)
        	{
        	    $this->AddAttachment($value);
        	}
        }
        else if (isset($attachments))
        {
        	$this->AddAttachment($attachments);
        }
        return PROPER_MAIL_ARGUMENT;        
    }
}
?>